package com.fcscs.cservice.common;

import java.util.List;

import com.fcscs.core.util.CommonUtil;

public class CServiceCommonUtils {

	public interface NotifEntityIntf {
//		long getMsgId();
		String getTokenValue(String token);
		List<NotifEntityIntf> getIterTokens(String token);
	}
	
	public static boolean isNullOrNegative(Long l) {
		if (l == null || l < 0) {
			return true;
		}
		return false;
	}

	public static void parseTemplate(String detail, NotifEntityIntf obj, StringBuilder sb) {
		if (detail == null || detail.isEmpty()) {
			return;
		}
		int ind = detail.indexOf(CServiceConstants.NOTIF_TMPL_DELIM_LEFT);
		if (ind >= 0) {
			sb.append(detail.substring(0, ind));
			if (CServiceConstants.NOTIF_TMPL_ITER_START.equals(detail.substring(ind, ind + CServiceConstants.NOTIF_TMPL_ITER_START.length()))) {
				int iterTokenStart = ind + CServiceConstants.NOTIF_TMPL_ITER_START.length();
				int iterTokenEnd = detail.indexOf(CServiceConstants.NOTIF_TMPL_ITER_ITEM_RIGHT, iterTokenStart);
				if (iterTokenEnd >= 0) {
					String iterTokenName = detail.substring(iterTokenStart, iterTokenEnd);
					int iterItemStart = iterTokenEnd + CServiceConstants.NOTIF_TMPL_ITER_ITEM_RIGHT.length();
					int iterItemEnd = iterItemStart + findIterEnd(detail.substring(iterItemStart));
					if (iterItemEnd >= 0) {
						String iterDetail = detail.substring(iterItemStart, iterItemEnd);
						List<NotifEntityIntf> objList = obj.getIterTokens(iterTokenName);
						if (objList != null) {
							for (NotifEntityIntf iterObj : objList) {
								parseTemplate(iterDetail, iterObj, sb);
							}
						}
						parseTemplate(detail.substring(iterItemEnd + CServiceConstants.NOTIF_TMPL_DELIM_RIGHT.length()), obj, sb);
					}
				}
			} else {
				int end = detail.indexOf(CServiceConstants.NOTIF_TMPL_DELIM_RIGHT, ind);
				if (end >= 0) {
					String tokenName = detail.substring(ind + CServiceConstants.NOTIF_TMPL_DELIM_LEFT.length(), end);
					if (CServiceConstants.NOTIF_TMPL_SENDER.equals(tokenName)) {
						String senderName = CServiceConstants.NO_SENDER;
						if (CommonUtil.getWsSessionUsername() != null) {
							senderName = CommonUtil.getWsSessionUsername();
						} else if (CommonUtil.getUserContext() != null && CommonUtil.getUserContext().getUsername() != null) {
							senderName = CommonUtil.getUserContext().getUsername();
						}
						sb.append(senderName);
					} else {
						sb.append(obj.getTokenValue(tokenName));
					}
					parseTemplate(detail.substring(end + CServiceConstants.NOTIF_TMPL_DELIM_RIGHT.length()), obj, sb);
				}
			}
		} else {
			sb.append(detail);
			return;
		}
	}

	private static int findIterEnd(String detail) {
		int leftCount = 0;
		int rightCount = 0;
		int fromInx = 0;
		int tempLeft = 0;
		int tempRight = 0;
		while (fromInx < detail.length()) {
			tempLeft = detail.indexOf(CServiceConstants.NOTIF_TMPL_DELIM_LEFT, fromInx);
			tempRight = detail.indexOf(CServiceConstants.NOTIF_TMPL_DELIM_RIGHT, fromInx);
			if (tempLeft >= 0 && (tempRight < 0 || tempLeft < tempRight)) {
				leftCount ++;
				fromInx = tempLeft + CServiceConstants.NOTIF_TMPL_DELIM_LEFT.length();
			} else if (tempRight >= 0 && (tempLeft < 0 || tempRight < tempLeft)) {
				rightCount ++;
				if (rightCount > leftCount) {
					return tempRight;
				}
				fromInx = tempRight + CServiceConstants.NOTIF_TMPL_DELIM_RIGHT.length();
			} else {
				return -1;
			}
		}
		return -1;
	}

	byte[] compactImageThumbnail(byte[] image) {
		return image; //TODO
	}
}
